<?php

class  Sistema_TrocaturmaController extends App_Controller_Action
{
    private $_table     = 'CURSO';
    private $_pk        = 'CUR_ID';
    private $_order_col = 'CUR_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'CUR_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        
    }

    public function indexAction() {
        $this->_helper->redirector('edit');
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_trocaturma.js');
        
        $aluno = new  Sistema_Model_DbTable_Aluno();
        $this->view->aluno = $aluno->fetchAll($aluno->select()->order('ALU_NOM ASC'));
        
        //$this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $data = array('CUR_ID'=> $formData['CUR_ID_TRM']);
                    
                    $where = 'ALU_ID = '.$formData['ALU_ID'].' AND CUR_ID = '.$formData['CUR_ID'];
                    
                    if($formData['CUR_ID_TRM'] > '' && $formData['ALU_ID'] > 0 && $formData['CUR_ID'] > 0){
                        $this->db->update('MATRICULA',$data, $where);
                        echo '<script>alert("Alterado com sucesso a turma!");</script>';
                    } else echo '<script>alert("Selecione corretamente as opções!");</script>';
                    
                    //$this->_helper->redirector('index');
                } else {
                    //$this->form->populate($formData);
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
        }       
        echo json_encode(array('ok' => $ok));
    }
    
    public function buscacursoAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $curso_html = '';
        $turma_html = '';
        $id = $this->_getParam('alu_id', 0);
        if ((int)$id > 0){   
            
            $ok = TRUE;
            try {
                $query = 'SELECT SQL_CACHE c.CUR_ID, c.CBS_ID, c.CUR_TRM, cb.CBS_NOM, n.NRT_NOM FROM MATRICULA m, CURSO c, CURSOBASE cb, NUMEROTURMA n '
                        . 'WHERE m.CUR_ID = c.CUR_ID AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID AND m.MAT_STA = 1 and m.ALU_ID = '.$id;
                $mat = $this->db->fetchRow($query);
                
                if($mat['CUR_ID'] > '')
                    $curso_html = '<input type="hidden" id="CUR_ID" name="CUR_ID" value="'.$mat['CUR_ID'].'" /> '.$mat['CBS_NOM'].' '.$mat['NRT_NOM'];

                $where = '';
                if($mat['CUR_ID'] > '') $where .= ' AND c.CUR_ID <> '.$mat['CUR_ID'];
                if($mat['CBS_ID'] > '') $where .= ' AND c.CBS_ID = '.$mat['CBS_ID'];
                if($mat['CUR_TRM'] > '') $where .= ' AND c.CUR_TRM <> '.$mat['CUR_TRM'];
                
                $query = 'SELECT SQL_CACHE c.CUR_ID,cb.CBS_NOM,c.CUR_TRM, n.NRT_NOM '
                        . 'FROM CURSO c, CURSOBASE cb, NUMEROTURMA n '
                        . 'WHERE c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID AND c.CUR_STA = 1 '
                        . $where.' ORDER BY n.NRT_SEM ASC, n.NRT_NOM ASC, c.CUR_ID ASC';
                $troca = $this->db->fetchAll($query);
                
                $turma_html .= '<ul class="listop">'; 
                foreach($troca as $t) {
                    $turma_html .= '<li><label><input type="radio" name="CUR_ID_TRM" value="'.$t['CUR_ID'].'" required="" /> '.$t['CBS_NOM'].' '.$t['NRT_NOM'].'</label></li>';
                }
                $turma_html .= '</ul>'; 
                
           } catch(Exception $ex) {
                $ok = FALSE;
            }
        }    
        
        echo json_encode(array('ok' => $ok,'curso'=>$curso_html,'turma'=>$turma_html));
    }
    
    
    

}
